<?php
declare(strict_types=1);

namespace Zlf\Request;

use Exception;

class Http
{
    /**
     * 发送GET数据
     * @param string $url
     * @param array $config
     * @return array
     * @author 竹林风@875384189 2021/8/3 13:10
     */
    public static function get(string $url, array $config = []): array
    {
        return self::send('GET', $url, null, $config);
    }

    /**
     * 发送POST数据
     * @param string $url
     * @param array|string|null $data
     * @param array $config
     * @return array
     * @author 竹林风@875384189 2021/8/3 13:10
     */
    public static function post(string $url, $data = null, array $config = [], array $file = []): array
    {
        return self::send('POST', $url, $data, $config, $file);
    }

    /**
     * 发送http请求
     * @param string $method
     * @param string $url
     * @param array|string|null $data
     * @param array $config
     * @return array
     * @author 竹林风@875384189 2021/8/3 13:09
     */
    public static function send(string $method, string $url, $data = null, array $config = [], array $file = []): array
    {
        try {
            $curl = curl_init();//开启CURL
            if ($method === 'GET') {
                curl_setopt($curl, CURLOPT_URL, $url);//设置请求地址
            } elseif ($method === 'POST') {
                curl_setopt($curl, CURLOPT_URL, $url);//设置请求地址
                curl_setopt($curl, CURLOPT_POST, 1);
                if ($data) {
                    curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
                }
                if ($file) {
                    curl_setopt($curl, CURLOPT_POSTFIELDS, $file);
                }
            }
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);//返回结果,不直接显示
            if (isset($config['headers']) && is_array($config['headers']) && count($config['headers']) > 0) {
                curl_setopt($curl, CURLOPT_HTTPHEADER, $config['headers']);
            }
            if (isset($config['cookie']) && $config['cookie']) {
                if (is_string($config['cookie'])) {
                    curl_setopt($curl, CURLOPT_COOKIE, $config['cookie']);
                } elseif (is_array($config['cookie'])) {
                    $cookie = [];
                    foreach ($config['cookie'] as $key => $value) {
                        $cookie[] = "$key=$value";
                    }
                    curl_setopt($curl, CURLOPT_COOKIE, implode(';', $cookie));
                }
            }
            //跳过https验证
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
            //是否返回响应头
            curl_setopt($curl, CURLOPT_HEADER, false);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
            $result = curl_exec($curl);
            $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);//获取HTTP状态码
            curl_close($curl);
            return ['state' => true, 'code' => (int)$httpCode, 'msg' => '请求成功', 'result' => $result];
        } catch (Exception $exception) {
            return ['state' => false, 'code' => 500, 'msg' => '请求失败', 'error' => $exception->getMessage()];
        }
    }
}